﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SimpleDesktop.Plugins.EveryThing;
using SimpleDesktop.Plugins.Readfile;
using SimpleDesktop.Plugins.SQLite;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
using Microsoft.Office.Interop.Word;

namespace SimpleDesktop.Winform
{
    public partial class SearchForm : Form
    {
        public SearchForm()
        {
            InitializeComponent();
            InitializeListView();

        }
        private void InitializeListView()
        {
            lvResults.View = System.Windows.Forms.View.Details;
            lvResults.Columns.Add("Name", 250, HorizontalAlignment.Left);
            lvResults.Columns.Add("Path", 500, HorizontalAlignment.Left);
            lvResults.Columns.Add("Type", 100, HorizontalAlignment.Left);

            //添加打开办法
            lvResults.DoubleClick += LvResults_DoubleClick;
        }
        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged_1(object sender, EventArgs e)
        {

        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string searchString = txtSearch.Text;
            SearchEverything64(searchString);
            SearchContent(searchString);

        }
        private void SearchEverything64(string searchString)
        {
            EveryThing64.Everything_SetSearchW(searchString);
            EveryThing64.Everything_SetMatchPath(true);
            EveryThing64.Everything_SetMatchCase(false);
            EveryThing64.Everything_SetMatchWholeWord(false);
            EveryThing64.Everything_SetRegex(false);
            EveryThing64.Everything_SetMax(1000); // 限制结果数量

            if (EveryThing64.Everything_Query(true))
            {
                int numResults = (int)EveryThing64.Everything_GetNumResults();
                lvResults.Items.Clear();

                for (uint i = 0; i < numResults; i++)
                {
                    StringBuilder resultPath = new StringBuilder(260);
                    EveryThing64.Everything_GetResultFullPathName(i, resultPath, 260);
                    string path = resultPath.ToString();

                    string name = System.IO.Path.GetFileName(path);
                    string type = EveryThing64.Everything_IsFolderResult(i) ? "Folder" : "File";

                    ListViewItem item = new ListViewItem(name);
                    item.SubItems.Add(path);
                    item.SubItems.Add(type);
                    lvResults.Items.Add(item);
                }
            }
            else
            {
                MessageBox.Show("查询失败。");
            }
        }

        private void SearchContent(string searchString)
        {
            var db = new IndexDatabase("fileIndex.db");
            var results = db.SearchFilesByContent(searchString);

            foreach (var result in results)
            {
                string name = System.IO.Path.GetFileName(result);
                string type = "File";

                ListViewItem item = new ListViewItem(name);
                item.SubItems.Add(result);
                item.SubItems.Add(type);
                lvResults.Items.Add(item);
            }
            db.CloseConnection();
        }

        private void LvResults_DoubleClick(object sender, EventArgs e)
        {
            if (lvResults.SelectedItems.Count > 0)
            {
                var selectedItem = lvResults.SelectedItems[0];
                string path = selectedItem.SubItems[1].Text;

                try
                {
                    Process.Start(new ProcessStartInfo(path) { UseShellExecute = true });
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"无法打开文件: {ex.Message}");
                }
            }
        }
        private void lvResults_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void btnBuildIndex_Click(object sender, EventArgs e)
        {
            using (FolderBrowserDialog folderDialog = new FolderBrowserDialog())
            {
                folderDialog.Description = "Select the folder to index";
                folderDialog.ShowNewFolderButton = false;

                if (folderDialog.ShowDialog() == DialogResult.OK)
                {
                    string directoryPath = folderDialog.SelectedPath;
                    BuildIndex(directoryPath);
                }
            }
        }

        private void BuildIndex(string directoryPath)
        {
            var db = new IndexDatabase("fileIndex.db");
            var files = Directory.GetFiles(directoryPath, "*.*", SearchOption.AllDirectories);

            foreach (var file in files)
            {
                string content = db.ExtractContent(file);
                if (!string.IsNullOrEmpty(content))
                {
                    db.AddFileToIndex(file, content);
                }
            }
            db.CloseConnection();
        }






    }
}
